package com.example.demo.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class RedisSync {

    private static int stock = 50;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("/redis")
    public String deduckStock() throws InterruptedException {
        //int stock = Integer.parseInt(redisTemplate.opsForValue().get("stock"));
        Map map = new HashMap<>();


        if (stock > 0) {
            stock = stock - 1;
            //redisTemplate.opsForValue().set("stock",realStock+"");
            System.out.println("扣减成功！" + stock);
        } else {
            System.out.println("扣减失败,库存不足！");
        }
        return "end";
    }
}
